﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebPortal.Models.EntityModels;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using WebPortal.Models.EntityModels;

namespace WebPortal.Models.ViewModel
{
    public class GoodsModelList
    {
        public List<GoodsModel> goods = new List<GoodsModel>();
        public List<Categories> categories = new List<Categories>();
    }

    public class GoodsList
    {
        public List<Goods> goods = new List<Goods>();
        public List<Categories> categories = new List<Categories>();
    }


    public class GoodsModel
    {
        public int GoodID { get; set; }
        public String CatName { get; set; }
        public int CatID { get; set; }
        public String Name { get; set; }
        public String Description { get; set; }
        public String Picture { get; set; }
        public int BasketID { get; set; }
        public int BasketCount { get; set; }

        public GoodsModel(Goods g)
        {
            GoodID = g.GoodID;
            Name = g.Name;
            Description = g.Description;
            Picture = g.Picture;
            CatID = g.CategoryID;
        }

        public GoodsModel()
        {

        }

        protected static String GetCatName(Goods g, List<Categories> cats)
        {
            String res = null;
            foreach (Categories c in cats)
                if (c.CategoryID == g.CategoryID)
                {
                    res = c.Name;
                    break;
                }
            return res;
        }

        protected static int GetCount(Goods g, List<int> basket)
        {
            int res = 0;
            foreach (int i in basket)
                if (i == g.GoodID)
                    res++;
            return res;
        }

        public static GoodsModelList GetGoods(List<Goods> goods, List<Categories> cats)
        {
            GoodsModelList list = new GoodsModelList();
            foreach(Goods g in goods) {
                GoodsModel good = new GoodsModel(g);
                good.CatName = GetCatName(g, cats);
                list.goods.Add(good);
            }
            list.categories = cats;
            return list;
        }

        public static GoodsModelList GetGoods(List<Goods> goods, List<Categories> cats, List<int> basket)
        {
            GoodsModelList list = new GoodsModelList();
            foreach (Goods g in goods)
            {
                GoodsModel good = new GoodsModel(g);
                good.CatName = GetCatName(g, cats);
                good.BasketCount = GetCount(g, basket);
                list.goods.Add(good);
            }
            list.categories = cats;
            return list;
        }

        public static List<GoodsModel> GetGoods(List<Goods> goods, List<int> basket)
        {
            List<GoodsModel> list = new List<GoodsModel>();
            foreach (Goods g in goods)
            {
                GoodsModel good = new GoodsModel(g);
                good.BasketCount = GetCount(g, basket);
                list.Add(good);
            }
            return list;
        }
    }
}